﻿using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using Sunny.UI;
using ZhZyz.Cad;
using ZhZyz.Gdal;

namespace WinForms
{
    public partial class Form1 : UIForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrower_Click(object sender, EventArgs e)
        {
            //浏览目录
            var dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK) txtCadPath.Text = dlg.SelectedPath;
        }

        private void txtCadPath_TextChanged(object sender, EventArgs e)
        {
            //搜索所有CAD文件
            if (Directory.Exists(txtCadPath.Text))
            {
                trFileList.Nodes.Clear();
                var dwgfiles = Directory.GetFiles(txtCadPath.Text, "*.dwg");
                foreach (var item in dwgfiles) trFileList.Nodes.Add(Path.GetFileName(item));
                gpbFileList.Text = $"文件列表-[{dwgfiles.Length}]";
            }
        }

        private void btnBrowerDb_Click(object sender, EventArgs e)
        {
            //浏览目录
            var dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK) txtOutDb.Text = dlg.SelectedPath;
        }

        private void txtOutDb_TextChanged(object sender, EventArgs e)
        {
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            //读取一个试试
            if (trFileList.Nodes.Count == 0) return;

            CadHelper helper = new CadHelper(txtCadPath.Text+"\\"+ trFileList.Nodes[0].Text);

            uiTextBox3.Lines = helper.GetPlByCass(txtCass.Text,uicmbFormat.SelectedIndex);
        }

        private void btnWrite_Click(object sender, EventArgs e)
        {
            //gdal
            //MyGdalHelper myGdal = new MyGdalHelper();
            //myGdal.AppendData(txtOutDb.Text, txtOutLayer.Text, uiTextBox3.Lines, uicmbFormat.SelectedIndex == 0);
            //aspose.gis
            MyGDBHelper.AppendData(txtOutDb.Text, txtOutLayer.Text, uiTextBox3.Lines);//aspose.gis 仅支持wkt

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtCadPath_TextChanged(sender, e);
            txtCass_TextChanged(sender, e);
            uicmbFormat.SelectedIndex = 0;

            CadHelper.ProcessEvent += CadHelper_ProcessEvent;
            MyGDBHelper.ProcessEvent += MyGDBHelper_ProcessEvent;
        }

        private void MyGDBHelper_ProcessEvent(string msg)
        {
            //写库事件
            this.Invoke(new Action(() => {
                uiTextBox3.AppendText(msg );
            uiTextBox3.ScrollToCaret();
            }));
        }

        private void CadHelper_ProcessEvent(string msg)
        {
            //CAD处理事件
            this.Invoke(new Action(()=> {
                uiTextBox3.AppendText(msg );
                uiTextBox3.ScrollToCaret();
            }));
         
        }

        private void txtCass_TextChanged(object sender, EventArgs e)
        {
            labCassDesc.Text = CadHelper.GetCassDesc(txtCass.Text);
            txtOutLayer.Text ="l_"+ txtCass.Text;
        }

        private void uiTextBox3_TextChanged(object sender, EventArgs e)
        {
            uiGroupBox1.Text =$"内容展示-[{uiTextBox3.Lines.Length}]" ;
        }

        private void uiButton1_Click_1(object sender, EventArgs e)
        {
            //浏览目录
            var dlg = new OpenFileDialog() { FileName="",Title="指定图层CASS列表",Filter="列表文件|*.txt"};
            if (dlg.ShowDialog() == DialogResult.OK) txtLayerCass.Text = dlg.FileName;
        }

        private void btnBatchRead_Click(object sender, EventArgs e)
        {
            if (txtLayerCass.Text == "")
            {
                MessageBox.Show("选择层码列表文件");
                return;
            }
            if (trFileList.Nodes.Count == 0)
            {
                MessageBox.Show("指定CAD文件所在路径");
                return;
            }
            new Thread(() => { CadHelper.GetAllByCass(txtLayerCass.Text, txtCadPath.Text); }).Start();
        
        }
        private void BatchReadAndWrite()
        {
            CadHelper.GetAllByCass(txtLayerCass.Text, txtCadPath.Text);
            MyGDBHelper.CreateDatas(txtOutDb.Text);
        }

        private void btnBatchWrite_Click(object sender, EventArgs e)
        {
            new Thread(()=> { MyGDBHelper.CreateDatas(txtOutDb.Text); }).Start();
        }

        private void btnGetAllCass_Click(object sender, EventArgs e)
        {
            new Thread(() => { CadHelper.GetAllCass(txtCadPath.Text); }).Start();
        }
    }
}